home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / emacs.lha / emacs-19.16 / lisp / informat.el < prev    next >
Lisp/Scheme  |  1993-02-15  |  14KB  |  424 lines

  1. ;;; informat.el --- info support functions package for Emacs
  2.  
  3. ;; Copyright (C) 1986 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: help
  7.  
  8. ;; This file is part of GNU Emacs.
  9.  
  10. ;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;; GNU General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Code:
  25.  
  26. (require 'info)
  27.  
  28. ;;;###autoload
  29. (defun Info-tagify ()
  30.   "Create or update Info-file tag table in current buffer."
  31.   (interactive)
  32.   ;; Save and restore point and restrictions.
  33.   ;; save-restrictions would not work
  34.   ;; because it records the old max relative to the end.
  35.   ;; We record it relative to the beginning.
  36.   (message "Tagifying %s ..." (file-name-nondirectory (buffer-file-name)))
  37.   (let ((omin (point-min))
  38.     (omax (point-max))
  39.     (nomax (= (point-max) (1+ (buffer-size))))
  40.     (opoint (point)))
  41.     (unwind-protect
  42.     (progn
  43.       (widen)
  44.       (goto-char (point-min))
  45.       (if (search-forward "\^_\nIndirect:\n" nil t)
  46.           (message "Cannot tagify split info file")
  47.         (let ((regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
  48.           (case-fold-search t)
  49.           list)
  50.           (while (search-forward "\n\^_" nil t)
  51.         (forward-line 1)
  52.         (let ((beg (point)))
  53.           (forward-line 1)
  54.           (if (re-search-backward regexp beg t)
  55.               (setq list
  56.                 (cons (list (buffer-substring
  57.                       (match-beginning 1)
  58.                       (match-end 1))
  59.                     beg)
  60.                   list)))))
  61.           (goto-char (point-max))
  62.           (forward-line -8)
  63.           (let ((buffer-read-only nil))
  64.         (if (search-forward "\^_\nEnd tag table\n" nil t)
  65.             (let ((end (point)))
  66.               (search-backward "\nTag table:\n")
  67.               (beginning-of-line)
  68.               (delete-region (point) end)))
  69.         (goto-char (point-max))
  70.         (insert "\^_\f\nTag table:\n")
  71.         (move-marker Info-tag-table-marker (point))
  72.         (setq list (nreverse list))
  73.         (while list
  74.           (insert "Node: " (car (car list)) ?\177)
  75.           (princ (car (cdr (car list))) (current-buffer))
  76.           (insert ?\n)
  77.           (setq list (cdr list)))
  78.         (insert "\^_\nEnd tag table\n")))))
  79.       (goto-char opoint)
  80.       (narrow-to-region omin (if nomax (1+ (buffer-size))
  81.                    (min omax (point-max))))))
  82.   (message "Tagifying %s ... done" (file-name-nondirectory (buffer-file-name))))
  83.  
  84. ;;;###autoload
  85. (defun Info-split ()
  86.   "Split an info file into an indirect file plus bounded-size subfiles.
  87. Each subfile will be up to 50,000 characters plus one node.
  88.  
  89. To use this command, first visit a large Info file that has a tag
  90. table.  The buffer is modified into a (small) indirect info file which
  91. should be saved in place of the original visited file.
  92.  
  93. The subfiles are written in the same directory the original file is
  94. in, with names generated by appending `-' and a number to the original
  95. file name.  The indirect file still functions as an Info file, but it
  96. contains just the tag table and a directory of subfiles."
  97.  
  98.   (interactive)
  99.   (if (< (buffer-size) 70000)
  100.       (error "This is too small to be worth splitting"))
  101.   (goto-char (point-min))
  102.   (search-forward "\^_")
  103.   (forward-char -1)
  104.   (let ((start (point))
  105.     (chars-deleted 0)
  106.     subfiles
  107.     (subfile-number 1)
  108.     (case-fold-search t)
  109.     (filename (file-name-sans-versions buffer-file-name)))
  110.     (goto-char (point-max))
  111.     (forward-line -8)
  112.     (setq buffer-read-only nil)
  113.     (or (search-forward "\^_\nEnd tag table\n" nil t)
  114.     (error "Tag table required; use M-x Info-tagify"))
  115.     (search-backward "\nTag table:\n")
  116.     (if (looking-at "\nTag table:\n\^_")
  117.     (error "Tag table is just a skeleton; use M-x Info-tagify"))
  118.     (beginning-of-line)
  119.     (forward-char 1)
  120.     (save-restriction
  121.       (narrow-to-region (point-min) (point))
  122.       (goto-char (point-min))
  123.       (while (< (1+ (point)) (point-max))
  124.     (goto-char (min (+ (point) 50000) (point-max)))
  125.     (search-forward "\^_" nil 'move)
  126.     (setq subfiles
  127.           (cons (list (+ start chars-deleted)
  128.               (concat (file-name-nondirectory filename)
  129.                   (format "-%d" subfile-number)))
  130.             subfiles))
  131.     ;; Put a newline at end of split file, to make Unix happier.
  132.     (insert "\n")
  133.     (write-region (point-min) (point)
  134.               (concat filename (format "-%d" subfile-number)))
  135.     (delete-region (1- (point)) (point))
  136.     ;; Back up over the final ^_.
  137.     (forward-char -1)
  138.     (setq chars-deleted (+ chars-deleted (- (point) start)))
  139.     (delete-region start (point))
  140.     (setq subfile-number (1+ subfile-number))))
  141.     (while subfiles
  142.       (goto-char start)
  143.       (insert (nth 1 (car subfiles))
  144.           (format ": %d" (car (car subfiles)))
  145.           "\n")
  146.       (setq subfiles (cdr subfiles)))
  147.     (goto-char start)
  148.     (insert "\^_\nIndirect:\n")
  149.     (search-forward "\nTag Table:\n")
  150.     (insert "(Indirect)\n")))
  151.  
  152. ;;;###autoload
  153. (defun Info-validate ()
  154.   "Check current buffer for validity as an Info file.
  155. Check that every node pointer points to an existing node."
  156.   (interactive)
  157.   (save-excursion
  158.     (save-restriction
  159.       (widen)
  160.       (goto-char (point-min))
  161.       (if (search-forward "\nTag table:\n(Indirect)\n" nil t)
  162.       (error "Don't yet know how to validate indirect info files: \"%s\""
  163.          (buffer-name (current-buffer))))
  164.       (goto-char (point-min))
  165.       (let ((allnodes '(("*")))
  166.         (regexp "Node:[ \t]*\\([^,\n\t]*\\)[,\t\n]")
  167.         (case-fold-search t)
  168.         (tags-losing nil)
  169.         (lossages ()))
  170.     (while (search-forward "\n\^_" nil t)
  171.       (forward-line 1)
  172.       (let ((beg (point)))
  173.         (forward-line 1)
  174.         (if (re-search-backward regexp beg t)
  175.         (let ((name (downcase
  176.                   (buffer-substring
  177.                     (match-beginning 1)
  178.                 (progn
  179.                   (goto-char (match-end 1))
  180.                   (skip-chars-backward " \t")
  181.                   (point))))))
  182.           (if (assoc name allnodes)
  183.               (setq lossages
  184.                 (cons (list name "Duplicate node-name" nil)
  185.                   lossages))
  186.               (setq allnodes
  187.                 (cons (list name
  188.                     (progn
  189.                       (end-of-line)
  190.                       (and (re-search-backward
  191.                         "prev[ious]*:" beg t)
  192.                            (progn
  193.                          (goto-char (match-end 0))
  194.                          (downcase
  195.                            (Info-following-node-name)))))
  196.                     beg)
  197.                   allnodes)))))))
  198.     (goto-char (point-min))
  199.     (while (search-forward "\n\^_" nil t)
  200.       (forward-line 1)
  201.       (let ((beg (point))
  202.         thisnode next)
  203.         (forward-line 1)
  204.         (if (re-search-backward regexp beg t)
  205.         (save-restriction
  206.           (search-forward "\n\^_" nil 'move)
  207.           (narrow-to-region beg (point))
  208.           (setq thisnode (downcase
  209.                    (buffer-substring
  210.                      (match-beginning 1)
  211.                      (progn
  212.                        (goto-char (match-end 1))
  213.                        (skip-chars-backward " \t")
  214.                        (point)))))
  215.           (end-of-line)
  216.           (and (search-backward "next:" nil t)
  217.                (setq next (Info-validate-node-name "invalid Next"))
  218.                (assoc next allnodes)
  219.                (if (equal (car (cdr (assoc next allnodes)))
  220.                   thisnode)
  221.                ;; allow multiple `next' pointers to one node
  222.                (let ((tem lossages))
  223.                  (while tem
  224.                    (if (and (equal (car (cdr (car tem)))
  225.                            "should have Previous")
  226.                     (equal (car (car tem))
  227.                            next))
  228.                    (setq lossages (delq (car tem) lossages)))
  229.                    (setq tem (cdr tem))))
  230.              (setq lossages
  231.                    (cons (list next
  232.                        "should have Previous"
  233.                        thisnode)
  234.                      lossages))))
  235.           (end-of-line)
  236.           (if (re-search-backward "prev[ious]*:" nil t)
  237.               (Info-validate-node-name "invalid Previous"))
  238.           (end-of-line)
  239.           (if (search-backward "up:" nil t)
  240.               (Info-validate-node-name "invalid Up"))
  241.           (if (re-search-forward "\n* Menu:" nil t)
  242.               (while (re-search-forward "\n\\* " nil t)
  243.             (Info-validate-node-name
  244.               (concat "invalid menu item "
  245.                   (buffer-substring (point)
  246.                             (save-excursion
  247.                               (skip-chars-forward "^:")
  248.                               (point))))
  249.               (Info-extract-menu-node-name))))
  250.           (goto-char (point-min))
  251.           (while (re-search-forward "\\*note[ \n]*[^:\t]*:" nil t)
  252.             (goto-char (+ (match-beginning 0) 5))
  253.             (skip-chars-forward " \n")
  254.             (Info-validate-node-name
  255.              (concat "invalid reference "
  256.                  (buffer-substring (point)
  257.                            (save-excursion
  258.                          (skip-chars-forward "^:")
  259.                          (point))))
  260.              (Info-extract-menu-node-name "Bad format cross-reference")))))))
  261.     (setq tags-losing (not (Info-validate-tags-table)))
  262.     (if (or lossages tags-losing)
  263.         (with-output-to-temp-buffer " *problems in info file*"
  264.           (while lossages
  265.         (princ "In node \"")
  266.         (princ (car (car lossages)))
  267.         (princ "\", ")
  268.         (let ((tem (nth 1 (car lossages))))
  269.           (cond ((string-match "\n" tem)
  270.              (princ (substring tem 0 (match-beginning 0)))
  271.              (princ "..."))
  272.             (t
  273.              (princ tem))))
  274.         (if (nth 2 (car lossages))
  275.             (progn
  276.               (princ ": ")
  277.               (let ((tem (nth 2 (car lossages))))
  278.             (cond ((string-match "\n" tem)
  279.                    (princ (substring tem 0 (match-beginning 0)))
  280.                    (princ "..."))
  281.                   (t
  282.                    (princ tem))))))
  283.         (terpri)
  284.         (setq lossages (cdr lossages)))
  285.           (if tags-losing (princ "\nTags table must be recomputed\n")))
  286.       ;; Here if info file is valid.
  287.       ;; If we already made a list of problems, clear it out.
  288.       (save-excursion
  289.         (if (get-buffer " *problems in info file*")
  290.         (progn
  291.           (set-buffer " *problems in info file*")
  292.           (kill-buffer (current-buffer)))))
  293.       (message "File appears valid"))))))
  294.  
  295. (defun Info-validate-node-name (kind &optional name)
  296.   (if name
  297.       nil
  298.     (goto-char (match-end 0))
  299.     (skip-chars-forward " \t")
  300.     (if (= (following-char) ?\()
  301.     nil
  302.       (setq name
  303.         (buffer-substring
  304.          (point)
  305.          (progn
  306.           (skip-chars-forward "^,\t\n")
  307.           (skip-chars-backward " ")
  308.           (point))))))
  309.   (if (null name)
  310.       nil
  311.     (setq name (downcase name))
  312.     (or (and (> (length name) 0) (= (aref name 0) ?\())
  313.     (assoc name allnodes)
  314.     (setq lossages
  315.           (cons (list thisnode kind name) lossages))))
  316.   name)
  317.  
  318. (defun Info-validate-tags-table ()
  319.   (goto-char (point-min))
  320.   (if (not (search-forward "\^_\nEnd tag table\n" nil t))
  321.       t
  322.     (not (catch 'losing
  323.        (let* ((end (match-beginning 0))
  324.           (start (progn (search-backward "\nTag table:\n")
  325.                 (1- (match-end 0))))
  326.           tem)
  327.          (setq tem allnodes)
  328.          (while tem
  329.            (goto-char start)
  330.            (or (equal (car (car tem)) "*")
  331.            (search-forward (concat "Node: "
  332.                        (car (car tem))
  333.                        "\177")
  334.                    end t)
  335.            (throw 'losing 'x))
  336.            (setq tem (cdr tem)))
  337.          (goto-char (1+ start))
  338.          (while (looking-at ".*Node: \\(.*\\)\177\\([0-9]+\\)$")
  339.            (setq tem (downcase (buffer-substring
  340.                      (match-beginning 1)
  341.                      (match-end 1))))
  342.            (setq tem (assoc tem allnodes))
  343.            (if (or (not tem)
  344.                (< 1000 (progn
  345.                  (goto-char (match-beginning 2))
  346.                  (setq tem (- (car (cdr (cdr tem)))
  347.                           (read (current-buffer))))
  348.                  (if (> tem 0) tem (- tem)))))
  349.            (throw 'losing 'y)))
  350.          (forward-line 1))
  351.        (or (looking-at "End tag table\n")
  352.            (throw 'losing 'z))
  353.        nil))))
  354.  
  355. ;;;###autoload
  356. (defun batch-info-validate ()
  357.   "Runs `Info-validate' on the files remaining on the command line.
  358. Must be used only with -batch, and kills Emacs on completion.
  359. Each file will be processed even if an error occurred previously.
  360. For example, invoke \"emacs -batch -f batch-info-validate $info/ ~/*.info\""
  361.   (if (not noninteractive)
  362.       (error "batch-info-validate may only be used -batch."))
  363.   (let ((version-control t)
  364.     (auto-save-default nil)
  365.     (find-file-run-dired nil)
  366.     (kept-old-versions 259259)
  367.     (kept-new-versions 259259))
  368.     (let ((error 0)
  369.       file
  370.       (files ()))
  371.       (while command-line-args-left
  372.     (setq file (expand-file-name (car command-line-args-left)))
  373.     (cond ((not (file-exists-p file))
  374.            (message ">> %s does not exist!" file)
  375.            (setq error 1
  376.              command-line-args-left (cdr command-line-args-left))) 
  377.           ((file-directory-p file)
  378.            (setq command-line-args-left (nconc (directory-files file)
  379.                           (cdr command-line-args-left))))
  380.           (t
  381.            (setq files (cons file files)
  382.              command-line-args-left (cdr command-line-args-left)))))
  383.       (while files
  384.     (setq file (car files)
  385.           files (cdr files))
  386.     (let ((lose nil))
  387.       (condition-case err
  388.           (progn
  389.         (if buffer-file-name (kill-buffer (current-buffer)))
  390.         (find-file file)
  391.         (buffer-disable-undo (current-buffer))
  392.         (set-buffer-modified-p nil)
  393.         (fundamental-mode)
  394.         (let ((case-fold-search nil))
  395.           (goto-char (point-max))
  396.           (cond ((search-backward "\n\^_\^L\nTag table:\n" nil t)
  397.              (message "%s already tagified" file))
  398.             ((< (point-max) 30000)
  399.              (message "%s too small to bother tagifying" file))
  400.             (t
  401.              (Info-tagify))))
  402.         (let ((loss-name " *problems in info file*"))
  403.           (message "Checking validity of info file %s..." file)
  404.           (if (get-buffer loss-name)
  405.               (kill-buffer loss-name))
  406.           (Info-validate)
  407.           (if (not (get-buffer loss-name))
  408.               nil ;(message "Checking validity of info file %s... OK" file)
  409.             (message "----------------------------------------------------------------------")
  410.             (message ">> PROBLEMS IN INFO FILE %s" file)
  411.             (save-excursion
  412.               (set-buffer loss-name)
  413.               (princ (buffer-substring (point-min) (point-max))))
  414.             (message "----------------------------------------------------------------------")
  415.             (setq error 1 lose t)))
  416.         (if (and (buffer-modified-p)
  417.              (not lose))
  418.             (progn (message "Saving modified %s" file)
  419.                (save-buffer))))
  420.         (error (message ">> Error: %s" (prin1-to-string err))))))
  421.       (kill-emacs error))))
  422.  
  423. ;;; informat.el ends here
  424.